/*
 * @Author: panda-hbd 10254515+panda-hbd@user.noreply.gitee.com
 * @Date: 2022-11-11 08:39:08
 * @LastEditors: 姚贤通 10254465+yao-xiantong@user.noreply.gitee.com
 * @LastEditTime: 2022-11-22 10:18:00
 * @FilePath: \cal-money-system\src\store\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createStore } from 'vuex'
import user from '../api/user'

export default createStore({
    state: {
        userType: 'teacher',
        //当前查看的讨论主题id
        currentDiscusstionId: sessionStorage.getItem("currentDiscusstionId") || 0,
        //当前查看的班级id
        currentClassId: sessionStorage.getItem("currentClassId") || 0,
        //当前查看的试卷id
        currentPaperId: sessionStorage.getItem("currentPaperId") || 0,
        //当前查看的发布的试卷id
        currentClassPaperId: sessionStorage.getItem("currentClassPaperId") || 0,

        users: sessionStorage.getItem("users") ? JSON.parse(sessionStorage.getItem("users")) : {},
    },
    getters: {
        getCurrentDiscusstionId: (state) => state.currentDiscusstionId,
        getCurrentClassId: (state) => state.currentClassId,
        getCurrentPaperId: (state) => state.currentPaperId,
        getCurrentClassPaperId: (state) => state.currentClassPaperId,
        getUserType: (state) => state.userType,
    },
    mutations: {

        setCurrentDiscusstionId: (state, data) => {
            state.currentDiscusstionId = data
            sessionStorage.setItem("currentDiscusstionId", data)
        },
        setCurrentClassId: (state, data) => {
            state.currentClassId = data
            sessionStorage.setItem("currentClassId", data)
        },
        setCurrentPaperId: (state, data) => {
            state.currentPaperId = data
            sessionStorage.setItem("currentPaperId", data)
        },

        setCurrentClassPaperId: (state, data) => {
            state.currentClassPaperId = data
            sessionStorage.setItem("currentClassPaperId", data)
        },
        queryById: (state,data) => {

            user.queryById(data).then((res)=>{


                if(res.code == 1){
                    state.users = res.data;
                    console.log(res.data);
                    sessionStorage.setItem("users",JSON.stringify(res.data))
                    if(res.data.role == 0){
                        state.userType='teacher';
                    }else{
                        state.userType='student';
                    }
                }


            })

        },

    },
    actions: {

        queryById(context,data){

            context.commit("queryById",data);
        }

    },
    modules: {}
})
    

   
   
